package me.xplabs.view 
{
	import flash.display.Sprite;
	import flash.utils.getQualifiedClassName;
	import me.xplabs.Globals;
	import me.xplabs.interfaces.IBasePanel;
	import me.xplabs.model.combatunit.BaseBuild;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.RaidButtonData;
	import me.xplabs.msg.NotificationFloatingBox;
	import me.xplabs.ui.baseui.Label;
	import me.xplabs.ui.baseui.Panel;
	import me.xplabs.utils.Utils;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class FloatingBoxMediator extends Mediator implements IBasePanel
	{
		private var _container:Sprite;
		private var _isInit:Boolean;
		public static var NAME:String;
		private var _label:Label;
		private var _bgPanel:Panel;
		private var _updateKey:int;
		public function FloatingBoxMediator(viewComponent:Object=null) 
		{
			NAME = getQualifiedClassName(this);
			super(NAME, viewComponent);
			
		}
		private function init():void
		{
			if (_isInit) return;
			_isInit = true;
			_container = new Sprite();
			
			_label = Globals.ui.label("", 240);
			_label.x = 10;
			_label.y = 10;
			_label.leading = 3;
			_bgPanel = Globals.ui.panel(_label.width, 100, 4);
			
			_container.addChild(_bgPanel);
			_container.addChild(_label);
			//_updateKey = -1;
			 //_updateKey = Globals.timeManager.addUpdate(updateMouseXY);
			 Globals.gameManager.addUpdate(updateMouseXY);
			
		}
		
		private function updateMouseXY():void 
		{
			if (_container.stage)
			{
				var mx:Number = _container.stage.mouseX;
				var my:Number = _container.stage.mouseY;
				if (mx < Globals.centerW) _container.x = mx + 20;
				else _container.x = mx - _bgPanel.width - 20;
				if (my < Globals.centerH) _container.y = my + 20;
				else _container.y = my - _bgPanel.height - 20;
			}
		}
		public function show():void 
		{
			init();
			if (!_container.parent) viewComponent.addChild(_container);
			//if (_updateKey == -1) _updateKey = Globals.timeManager.addUpdate(updateMouseXY);
		}
		
		public function hide():void 
		{
			if (_container.parent) _container.parent.removeChild(_container);
			//if (_updateKey != -1) Globals.timeManager.removeUpdate(_updateKey);
		}
		override public function handleNotification(notification:INotification):void 
		{
			if (NotificationFloatingBox.HIDE_FLOATING_BOX == notification.getName())
			{
				hide();
				return;
			}
			show();
			switch(notification.getName())
			{
				case NotificationFloatingBox.SHOW_BUILD:
					showBuild(BaseBuild(notification.getBody()));
					break;
				case NotificationFloatingBox.SHOW_LOCK_BUILD:
					showLockBuild();
					break;
				case NotificationFloatingBox.SHOW_TOPLAYER_BUILD:
					showTopLayerBuild();
					break;
				case NotificationFloatingBox.SHOW_FLAG_INFO:
					showFlag(BaseFlag(notification.getBody()));
					break;
				case NotificationFloatingBox.SHOW_RAID_BUTTON:
					showRaidButton(RaidButtonData(notification.getBody()));
					break;
			}
		}
		/**
		 * 显示选择关卡的关卡信息
		 * @param	raidButtonData
		 */
		private function showRaidButton(raidButtonData:RaidButtonData):void 
		{
			_label.setWH(180, 0);
			_label.text = "";
			_label.textField.htmlText = "";
			_label.appendHtml(Utils.strToHtml(raidButtonData.name, "#ECEADB", 14, true));
			_label.appendHtml(Utils.strToHtml("<br></br>"));
			_label.appendHtml(Utils.strToHtml(raidButtonData.describe, "#F2E709"));
			_bgPanel.setWH(_label.width + 20, _label.height + 20);
		}
		/**
		 * 显示建筑的基本信息悬浮框
		 * @param	baseBuild
		 */
		private function showBuild(baseBuild:BaseBuild):void 
		{
			_label.setWH(180, 0);
			_label.text = "";
			_label.textField.htmlText = "";
			_label.appendHtml(Utils.strToHtml(baseBuild.title, "#ECEADB", 14, true));
			_label.appendHtml(Utils.strToHtml(baseBuild.describe, "#F2E709"));
			_label.appendHtml(Utils.strToHtml("<br></br>"));
			_label.appendHtml(Utils.strToHtml("伤害:", "#F2E709") + Utils.strToHtml(String(baseBuild.attack), "#ECEADB"));
			_label.appendHtml(Utils.strToHtml("攻速:", "#F2E709") + Utils.strToHtml(baseBuild.cdGap + "s", "#ECEADB"));
			_bgPanel.setWH(_label.width + 20, _label.height + 20);
		}
		/**
		 * 显示建筑锁定的悬浮框
		 */
		private function showLockBuild():void
		{
			_label.setWH(180, 0);
			_label.text = "";
			_label.textField.htmlText = "";
			_label.appendHtml(Utils.strToHtml("锁定！", "#ECEADB", 14, true));
			_label.appendHtml(Utils.strToHtml("此项已被锁定", "#F2E709", 12));
			_bgPanel.setWH(_label.width + 20, _label.height + 20);
		}
		/**
		 * 显示建筑已达顶级的悬浮框
		 */
		private function showTopLayerBuild():void
		{
			_label.setWH(140, 0);
			_label.text = "";
			_label.textField.htmlText = "";
			//_label.appendHtml(Utils.strToHtml("顶级！", "#ECEADB", 14, true));
			//_label.appendHtml(Utils.strToHtml("此项到达顶级", "#F2E709", 12));
			_label.appendHtml(Utils.strToHtml("锁定！", "#ECEADB", 14, true));
			_label.appendHtml(Utils.strToHtml("此项已被锁定", "#F2E709", 12));
			_bgPanel.setWH(_label.width + 20, _label.height + 20);
		}
		private function showFlag(baseFlag:BaseFlag):void
		{
			_label.setWH(180, 0);
			_label.text = "";
			_label.textField.htmlText = "";
			_label.appendHtml(Utils.strToHtml(baseFlag.title, "#ECEADB", 14, true));
			_label.appendHtml(Utils.strToHtml(baseFlag.describe, "#F2E709"));
			_label.appendHtml(Utils.strToHtml("<br></br>"));
			_label.appendHtml(Utils.strToHtml("等级:", "#F2E709") + Utils.strToHtml(String(baseFlag.flagLevel), "#ECEADB"));
			_label.appendHtml(Utils.strToHtml("范围:", "#F2E709") + Utils.strToHtml(String(baseFlag.areaW), "#ECEADB"));
			_bgPanel.setWH(_label.width + 20, _label.height + 20);
		}
		override public function listNotificationInterests():Array 
		{
			return [NotificationFloatingBox.SHOW_BUILD, 
					NotificationFloatingBox.HIDE_FLOATING_BOX,
					NotificationFloatingBox.SHOW_LOCK_BUILD,
					NotificationFloatingBox.SHOW_FLAG_INFO,
					NotificationFloatingBox.SHOW_TOPLAYER_BUILD,
					NotificationFloatingBox.SHOW_RAID_BUTTON];
		}
		public function dispose():void 
		{
			
		}
		
	}

}